﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Mysoft.App.Appservices.Basic;
using Mysoft.Domain;
using Mysoft.Domain.Basic;
using Mysoft.Dto;

namespace Mysoft.App.Appservices
{
    /// <summary>
    /// 字典应用服务
    /// </summary>
    public class DictionaryAppService : AppServiceBase
    {
        private readonly DomainService<DictionaryDomainService> _domainService;
        private readonly IMapper _mapper;
        /// <summary>
        /// 
        /// </summary>
        public DictionaryAppService(DomainService<DictionaryDomainService> domainService, IMapper mapper)
        {
            _domainService = domainService;
            _mapper=mapper;
        }



        /// <summary>
        /// 获取所有字典
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public virtual JsonResult GetDictList() 
        {
            var value = _domainService.Instance.GetDictList().Result;
            return Success(value);

        }
        /// <summary>
        /// 获取所有字典
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public virtual JsonResult GetDict()
        {
            var value = _domainService.Instance.GetDict().Result;
            return Success(value);
        }
        /// <summary>
        /// 新增字典集合
        /// </summary>
        /// <param name="dictionaryDto"></param>
        /// <returns></returns>
        [HttpPost]
        public virtual JsonResult AddDict(DictionaryDto dictionary)
        {
            if (dictionary == null) 
            {
                BusinessException("dictionary参数为空");
            }
            var value = _domainService.Instance.AddDict(dictionary).Result;
            return Success(value);
        }
        /// <summary>
        /// 新增字典集合
        /// </summary>
        /// <param name="dictionaryDto"></param>
        /// <returns></returns>
        [HttpPost]
        public virtual JsonResult AddDicts(List<DictionaryDto> dictionarys)
        {
            if (!dictionarys.Any())
            {
                BusinessException("dictionarys参数为空");
            }
            _domainService.Instance.AddDicts(dictionarys).Wait();
            return Success("新增成功");
        }
        /// <summary>
        /// 删除字典
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public virtual JsonResult DeleteDict(Guid? id)
        {
            if (!id.HasValue || id == Guid.Empty)
            {
                BusinessException("id参数为空");
            }
            _domainService.Instance.DeleteDict(id.Value);
            return Success("删除成功");
        }
     }
}
